package co.fitstart.admin.entity.content;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.schedule.ScheduleStatus;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author: Chang Liu
 * 
 */
public class ExpiryScheduleSearch implements QueryBuilder{
    
    private String name;
    
    private String mobile;
    
    private String courseType;
    
    private Long site;
    
    private List<Site> sites;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date targetDate;
    
    private Order order;
    
    private Filter filter;

    public ExpiryScheduleSearch(){
        order = new Order("occurDate", "DESC");
    }
    
    public Filter getPreparedFilter(boolean refresh) {
        if(filter == null || refresh){
            filter = new Filter().addSentence("courseSchedule.site in (:sites)", "sites", sites)
                                 .addLike("courseSchedule.student.name", name)
                                 .addEquals("courseSchedule.student.mobile", mobile)
                                 .addEquals("courseSchedule.courseType", CourseType.aliasOf(courseType))
                                 .addEquals("courseSchedule.site.id", site)
                                 .addGreaterThanEqual("courseSchedule.occurDate", this.getBeginDate())
                                 .addLessThanEqual("courseSchedule.occurDate", this.getEndDate())
                                 .addEquals("courseSchedule.scheduleStatus", ScheduleStatus.aliasOf("expiry"));
        }
        return filter;
    }

    public Order getOrder() {
        return order;
    }

    public String buildSentenceTemplate() {
        return "From CourseSchedule courseSchedule where courseSchedule.disabled = false " + getPreparedFilter(false).getClauseSentence() + order.getSentence();
    }

    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }
    
    public Date getBeginDate() {
        return DateUtils.getFirstWeekDay(this.targetDate);
    }
    
    public Date getEndDate() {
        return DateUtils.getLastWeekDay(this.targetDate);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getCourseType() {
        return courseType;
    }

    public void setCourseType(String courseType) {
        this.courseType = courseType;
    }

    public Long getSite() {
        return site;
    }

    public void setSite(Long site) {
        this.site = site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }

    public Date getTargetDate() {
        return targetDate;
    }

    public void setTargetDate(Date targetDate) {
        this.targetDate = targetDate;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}
